﻿using AutoMapper;
using System.Collections.Generic;

namespace BanquetGift.Command
{
    public static class AutoMapperHelp
    {
        /// <summary>
        /// 类型映射,默认字段名字一一对应
        /// </summary>
        /// <typeparam name="TSource">要被转化的实体，Entity</typeparam>
        /// <typeparam name="TDestination">转化之后的model，可以理解为viewmodel</typeparam>
        /// <param name="source">原对象</param>
        /// <returns>目标对象</returns>
        public static TDestination MappingTo<TSource, TDestination>(this TSource source)
            where TDestination : class
            where TSource : class
        {
            if (source == null) return default(TDestination);
            MapperConfiguration mapperConfiguration = new MapperConfiguration(
                mapperConfigurationExpression => mapperConfigurationExpression.CreateMap<TSource, TDestination>()
                );
            IMapper mapper = mapperConfiguration.CreateMapper();
            return mapper.Map<TDestination>(source);
        }

        /// <summary>
        /// 类型映射,默认字段名字一一对应
        /// </summary>
        /// <typeparam name="TDestination">转化之后的model，可以理解为viewmodel</typeparam>
        /// <param name="source">原对象</param>
        /// <returns>目标对象</returns>
        public static TDestination MappingTo<TDestination>(this object source)
            where TDestination : class
        {
            if (source == null) return default(TDestination);
            MapperConfiguration mapperConfiguration = new MapperConfiguration(
                mapperConfigurationExpression => mapperConfigurationExpression.CreateMap(source.GetType(), typeof(TDestination))
                );
            IMapper mapper = mapperConfiguration.CreateMapper();
            return mapper.Map<TDestination>(source);
        }

        /// <summary>
        /// 类型映射,默认字段名字一一对应
        /// </summary>
        /// <typeparam name="TSource">要被转化的实体，Entity</typeparam>
        /// <typeparam name="TDestination">转化之后的model，可以理解为viewmodel</typeparam>
        /// <param name="source">原对象</param>
        /// <param name="target">目标对象</param>
        public static void MappingTo<TSource, TDestination>(this TSource source, ref TDestination target)
            where TDestination : class
            where TSource : class
        {
            if (source == null)
            {
                target = default(TDestination);
                return;
            }
            MapperConfiguration mapperConfiguration = new MapperConfiguration(
                mapperConfigurationExpression => mapperConfigurationExpression.CreateMap<TSource, TDestination>()
                );
            IMapper mapper = mapperConfiguration.CreateMapper();
            target = mapper.Map<TDestination>(source);
        }

        /// <summary>
        /// 类型映射,默认字段名字一一对应
        /// </summary>
        /// <typeparam name="TDestination">转化之后的model，可以理解为viewmodel</typeparam>
        /// <param name="source">原对象</param>
        /// <param name="target">目标对象</param>
        public static void MappingTo<TDestination>(this object source, ref TDestination target)
            where TDestination : class
        {
            if (source == null)
            {
                target = default(TDestination);
                return;
            }
            MapperConfiguration mapperConfiguration = new MapperConfiguration(
                mapperConfigurationExpression => mapperConfigurationExpression.CreateMap(source.GetType(), typeof(TDestination))
                );
            IMapper mapper = mapperConfiguration.CreateMapper();
            target = mapper.Map<TDestination>(source);
        }

        /// <summary>
        /// 集合列表类型映射,默认字段名字一一对应
        /// </summary>
        /// <typeparam name="TSource">要被转化的实体，Entitys</typeparam>
        /// <typeparam name="TDestination">转化之后的model，可以理解为viewmodels</typeparam>
        /// <param name="sources">原对象列表</param>
        /// <returns>目标对象列表</returns>
        public static IEnumerable<TDestination> MappingTo<TSource, TDestination>(this IEnumerable<TSource> sources)
            where TDestination : class
            where TSource : class
        {
            if (sources == null) return default(IEnumerable<TDestination>);
            MapperConfiguration mapperConfiguration = new MapperConfiguration(mapperConfigurationExpression =>
                mapperConfigurationExpression.CreateMap<TSource, TDestination>()
                );
            IMapper mapper = mapperConfiguration.CreateMapper();
            return mapper.Map<List<TDestination>>(sources);
        }

        /// <summary>
        /// 集合列表类型映射,默认字段名字一一对应
        /// </summary>
        /// <typeparam name="TSource">要被转化的实体，Entitys</typeparam>
        /// <typeparam name="TDestination">转化之后的model，可以理解为viewmodels</typeparam>
        /// <param name="sources">原对象列表</param>
        /// <param name="targets">目标对象列表</param>
        public static void MappingTo<TSource, TDestination>(this IEnumerable<TSource> sources, ref IEnumerable<TDestination> targets)
            where TDestination : class
            where TSource : class
        {
            if (sources == null)
            {
                targets = default(IEnumerable<TDestination>);
                return;
            }
            MapperConfiguration mapperConfiguration = new MapperConfiguration(
                mapperConfigurationExpression => mapperConfigurationExpression.CreateMap<TSource, TDestination>()
                );
            IMapper mapper = mapperConfiguration.CreateMapper();
            targets = mapper.Map<IEnumerable<TDestination>>(sources);
        }
    }
}
